// pages/order/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '0',
            'title': '海螺号工蜂',
            'color': true,
            'class': false
        },
        imagesURL: app.imagesURL,
        dateSelector: app.MultipleSelectDate,
        pageHeight: app.globalData.pageHeight, //手机屏幕高度
        navHeight: app.globalData.navHeight,
        multiIndex: [0, 0, 0, 0, 0, 0],
        isBillDetail: false, // 订单分账弹窗
        billDetailInfo: {}, //订单分账详情
        isCoverUpDetail: false, // 补仓弹窗
        coverUpDetailInfo: {}, //补仓详情
        navH: '',
        date: '自定义',
        days: 30,
        startTime: '',
        endTime: '',
        dealerId: '',
        billForm: {
            cha: '',
            offset: 1,
            pageSize: 10
        },
        coverUpForm: {
            cha: '',
            orderNo: '',
            status: '',
            offset: 1,
            pageSize: 10
        },
        isNav: 1, //d顶部tab切换
        footerInfo: {
            countbPurchaseOrderId: 0,
            totalSalesmanIncomeMoney: 0
        },
        statusNum: {
            stateThree: 0,
            stateZero: 0
        }, //代发货和待审核数量
        dialog: false,
        allShop: false,
        activeShop: '全部店铺',
        allShopInfo: [],
        scrollTop: 0,
        shopBillInfo: [], //分账列表
        coverUpInfo: {} //补仓
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        var cha = wx.getStorageSync('orederValue');
        if (cha == undefined && cha == null) {
            cha = ''
        }
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                that.setData({
                    salesmanId: res.data.id,
                    navH: app.globalData.navHeight
                })
                if (wx.getStorageSync('isNav') == 2) {
                    that.setData({
                        isNav: 2,
                        'coverUpForm.cha': cha
                    })
                    that.getCoverUp()
                    that.getNum()
                } else {
                    that.setData({
                        isNav: 1,
                        'billForm.cha': cha
                    })
                    //订单与分账
                    that.getShopBill()
                }
                /* 加载小计 */
                that.getStatistics()
                /* 全部 */
                that.getAllShop()

            }
        })
    },

    /* 
      方法
    */
    /* 顶部的tab切换 */
    handleTop(e) {

        this.setData({
            isNav: e.currentTarget.dataset.item,
            'coverUpForm.cha': '',
            'billForm.cha': '',
            scrollTop: 0
        })
        // 1 --订单与分账  2---补仓单
        if (e.currentTarget.dataset.item == 1) {
            this.getShopBill()
        } else {
            this.getCoverUp()
            this.getNum()
        }
    },
    /* 点击状态查询 */
    handleStatus(e) {
        this.setData({
            'coverUpForm.status': e.currentTarget.dataset.status
        })
        this.getCoverUp()
    },
    /* 选择日期 */
    handleDate(e) {
        if (e.currentTarget.dataset.item == 0) {
            this.setData({
                days: 0,
                startTime: '',
                endTime: '',
                date: '自定义',
                'coverUpForm.offset': 1,
                'billForm.offset': 1
            })
        } else {
            this.setData({
                days: e.currentTarget.dataset.item,
                startTime: '',
                endTime: '',
                date: '自定义',
                'coverUpForm.offset': 1,
                'billForm.offset': 1
            })
        }

        if (this.data.isNav == 1) {
            this.getShopBill()
            this.getStatistics()
        } else {
            this.getCoverUp()
        }
    },
    /* 滚动到底部加载 */
    lowerSplitAccount(e) {
        this.setData({
            'billForm.offset': this.data.billForm.offset + 1
        })
        this.getShopBill()
    },
    lowerReplenishment(e) {
        this.setData({
            'coverUpForm.offset': this.data.coverUpForm.offset + 1
        })
        this.getCoverUp()
    },
    /* 佣金小计，统计 */
    getStatistics() {
        var that = this;
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'service',
            a: 'findTotalCommissionAndOrder/',
            q: {
                salesmanId: that.data.salesmanId, //业务员id
                days: that.data.days,
                startTime: that.data.startTime,
                endTime: that.data.endTime
            }
        }), res => {}, (res) => {
            that.setData({
                footerInfo: res.data[0]
            })
        })
    },
    /* 全部店铺 */
    getAllShop() {
        var that = this;
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'service',
            a: 'findAllDealer/',
            q: {
                salesmanId: that.data.salesmanId, //业务员id
            }
        }), res => {}, (res) => {
            that.setData({
                allShopInfo: res.data
            })
        })
    },
    /* 显示全部店铺 */
    showAllShop() {
        this.setData({
            dialog: true,
            allShop: true
        })
    },
    /* 选择店铺 */
    handleShop(e) {
        if (e.currentTarget.dataset.item == '') {
            this.setData({
                dialog: false,
                allShop: false,
                activeShop: '全部店铺',
                dealerId: '',
                'billForm.offset': '1'
            })
        } else {
            this.setData({
                dialog: false,
                allShop: false,
                activeShop: e.currentTarget.dataset.item.dealerName,
                dealerId: e.currentTarget.dataset.item.dealerId,
                'billForm.offset': '1'
            })
        }
        this.getShopBill()
        this.getCoverUp()
        this.getNum()

    },
    /* 店铺订单与分账 */
    getShopBill() {
        var that = this;
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'service',
            a: 'findOrdersAndSplitAccounts/',
            q: {
                salesmanId: that.data.salesmanId, //业务员id
                dealerId: that.data.dealerId,
                days: that.data.days,
                startTime: that.data.startTime,
                endTime: that.data.endTime,
                cha: that.data.billForm.cha,
                offset: that.data.billForm.offset,
                pageSize: that.data.billForm.pageSize
            }
        }), res => {}, (res) => {
            var data = res.data.list
            if (that.data.billForm.offset != 1) {
                data = that.data.shopBillInfo.concat(data)
            }
            that.setData({
                shopBillInfo: data,
                'billForm.offset': res.data.pageNum
            })
        })
    },
    /* 订单分账详情 */
    handleBillDetail(e) {
        var that = this;
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'service',
            a: 'OrderAndDimension/',
            q: {
                retailOrderId: e.currentTarget.dataset.id
            }
        }), res => {}, (res) => {
            that.setData({
                billDetailInfo: res.data,
                dialog: true,
                isBillDetail: true
            })
        })
    },
    // 1 --订单与分账  2---补仓单
    // 跳转搜素哦
    handleSearch() {
        wx.navigateTo({
            url: "/pages/order/search/index?orderType=" + this.data.isNav
        })
    },
    /* 关闭订单分账弹出 */
    closeBillDetail() {
        this.setData({
            dialog: false,
            isBillDetail: false
        })
    },
    closeCoverUpDetail() {
        this.setData({
            dialog: false,
            isCoverUpDetail: false
        })
    },
    /* 店铺补仓单 */
    getCoverUp() {
        var that = this;
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'service',
            a: 'findReplenishmentOrderDetails/',
            q: {
                salesmanId: that.data.salesmanId, //业务员id
                state: that.data.coverUpForm.status,
                orderNo: that.data.coverUpForm.orderNo,
                dealerId: that.data.dealerId,
                days: that.data.days,
                startTime: that.data.startTime,
                endTime: that.data.endTime,
                cha: that.data.coverUpForm.cha,
                offset: that.data.coverUpForm.offset,
                pageSize: that.data.coverUpForm.pageSize
            }
        }), res => {}, (res) => {
            var data = res.data.list
            if (that.data.coverUpForm.offset != 1) {
                data = that.data.coverUpInfo.concat(data)
            } else {
                data = res.data.list
            }
            that.setData({
                coverUpInfo: data,
                'coverUpForm.offset': res.data.pageNum
            })

        })
    },
    /* 补仓单详情 */
    handleCoverUpDetail(e) {
        var that = this;
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'service',
            a: 'findReplenishmentOrder/',
            q: {
                orderNo: e.currentTarget.dataset.id
            }
        }), res => {}, (res) => {
            that.setData({
                coverUpDetailInfo: res.data,
                dialog: true,
                isCoverUpDetail: true
            })
        })
    },
    /* 待审核和代发货数量 */
    getNum() {
        var that = this;
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'service',
            a: 'findTotalReviewedShipped/',
            q: {
                salesmanId: that.data.salesmanId, //业务员id
                dealerId: that.data.dealerId
            }

        }), res => {}, (res) => {
            that.setData({
                statusNum: res.data[0]
            })
        })
    },
    /* 自定义时间 */
    bindMultiPickerChange: function (e) {
        var startTime = this.data.dateSelector[0][e.detail.value[0]] + '-' + this.data.dateSelector[1][e.detail.value[1]] + '-' + this.data.dateSelector[2][e.detail.value[2]]
        var endTime = this.data.dateSelector[3][e.detail.value[3]] + '-' + this.data.dateSelector[4][e.detail.value[4]] + '-' + this.data.dateSelector[5][e.detail.value[5]]
        this.setData({
            date: this.data.dateSelector[1][e.detail.value[1]] + '-' + this.data.dateSelector[2][e.detail.value[2]] + '-' + this.data.dateSelector[4][e.detail.value[4]] + '-' + this.data.dateSelector[5][e.detail.value[5]],
            multiIndex: e.detail.value,
            days: '',
            startTime: startTime,
            endTime: endTime
        })
        // 1 --订单与分账  2---补仓单
        if (this.data.isNav == 1) {
            this.getShopBill()
            this.getStatistics()
        } else {
            this.getCoverUp()
            this.getNum()
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        // 删除缓存
        wx.removeStorageSync('isNav')
        wx.removeStorageSync('orederValue')

    },
    // 发送给朋友
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
        }
        return {
            title: '海螺号工蜂',
            path: 'pages/logs/logs'
        }
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        // 删除缓存
        wx.removeStorageSync('isNav')
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})